package com.timmy.google_play.base;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;

import com.timmy.google_play.R;
import com.timmy.google_play.proxy.ThreadPoolProxy;
import com.timmy.google_play.utils.UIUtils;

/**
 * Created by Timmy on 2016/12/8.
 * 作用:
 * 1.提供视图
 * 2.接收/加载数据
 * 3.数据和视图的绑定
 */

public abstract class LoadingPager extends FrameLayout {

    public static final int STATE_LOADING = 0;//加载中
    public static final int STATE_ERROR   = 1;//错误视图
    public static final int STATE_EMPTY   = 2;//空视图
    public static final int STATE_SUCCESS = 3;//成功视图
    public              int mCurState     = STATE_LOADING;

    private View         mEmptyView;
    private View         mErrorView;
    private View         mLoadingView;
    private View         mSuccessView;
    private LoadDataTask mLoadDataTask;

    public LoadingPager(Context context) {
        super(context);

        initCommonView();

    }

    /**
     * 创建三个静态视图
     */
    private void initCommonView() {

        //空视图
        mEmptyView = View.inflate(UIUtils.getContext(), R.layout.pager_empty, null);
        this.addView(mEmptyView);

        //错误视图
        mErrorView = View.inflate(UIUtils.getContext(), R.layout.pager_error, null);
        this.addView(mErrorView);

        //错误视图中设置点击事件
        mErrorView.findViewById(R.id.error_btn_retry).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //重新触发加载数据
                triggerLoadData();
            }
        });

        //加载中视图
        mLoadingView = View.inflate(UIUtils.getContext(), R.layout.pager_loading, null);
        this.addView(mLoadingView);

        //刷新ui视图
        refreshUIByState();

    }

    private void refreshUIByState() {

        //隐藏所有的视图
        mEmptyView.setVisibility(GONE);
        mErrorView.setVisibility(GONE);
        mLoadingView.setVisibility(GONE);

        if (mSuccessView != null) {
            mSuccessView.setVisibility(GONE);
        }

        switch (mCurState) {
            case STATE_LOADING:     //加载中视图
                mLoadingView.setVisibility(VISIBLE);
                break;
            case STATE_EMPTY:       //空视图
                mEmptyView.setVisibility(VISIBLE);
                break;
            case STATE_ERROR:       //错误视图
                mErrorView.setVisibility(VISIBLE);
                break;
            case STATE_SUCCESS:     //成功视图
                // 显示成功视图

                if (mSuccessView == null) {
                    mSuccessView = initSuccessView();
                    this.addView(mSuccessView);
                }

                //显示成功视图
                mSuccessView.setVisibility(VISIBLE);

                break;
        }

    }

    //触发加载数据
    public void triggerLoadData() {

        //如果当前状态不等于成功视图
        if (mCurState != STATE_SUCCESS && mLoadDataTask == null) {

            //进行触发加载数据,重置状态
            mCurState = STATE_LOADING;
            refreshUIByState();

            //进行异步加载数据
            mLoadDataTask = new LoadDataTask();
            // new Thread(mLoadDataTask).start();

            //使用线程池代理
            new ThreadPoolProxy(5).submit(mLoadDataTask);
        }
    }

    class LoadDataTask implements Runnable {
        @Override
        public void run() {
            //子线程.得到数据
            LoadedResult loadedResult = initData();

            //处理数据
            mCurState = loadedResult.getState();

            //刷新ui,根据加载数据返回的结果,来判断加载什么视图
            MyApplication.getHandler().post(new Runnable() {
                @Override
                public void run() {
                    refreshUIByState();
                }
            });

            //任务执行完成,置空任务
            mLoadDataTask = null;
        }
    }

    //子线程中请求数据
    public abstract LoadedResult initData();

    //返回的是成功视图
    public abstract View initSuccessView();


    public enum LoadedResult {

        SUCCESS(STATE_SUCCESS), EMPTY(STATE_EMPTY), ERROR(STATE_ERROR);

        public int state;

        public int getState() {
            return state;
        }

        LoadedResult(int state) {

            this.state = state;
        }
    }
}
